#!/usr/bin/env python3
"""
perl-Module-ScanDeps MCP Server

面向软件：perl-Module-ScanDeps（scandeps.pl 命令行工具）

所有 MCP tool 列表：
- scandeps_bundle: 生成捆绑包格式的依赖分析
  参数：module_file - Perl 模块文件路径
  返回：统一 JSON 结构

- scandeps_verbose: 生成详细格式的依赖分析
  参数：module_file - Perl 模块文件路径
  返回：统一 JSON 结构

- scandeps_trace: 生成跟踪格式的依赖分析
  参数：module_file - Perl 模块文件路径
  返回：统一 JSON 结构

- scandeps_executable: 分析可执行文件的依赖
  参数：module_file - Perl 模块文件路径
  返回：统一 JSON 结构

- scandeps_xargs: 传递额外参数给模块
  参数：module_file - Perl 模块文件路径, xargs - 额外参数字符串
  返回：统一 JSON 结构

- scandeps_check: 检查模块语法
  参数：module_file - Perl 模块文件路径
  返回：统一 JSON 结构

- scandeps_recursive: 递归分析依赖
  参数：module_file - Perl 模块文件路径
  返回：统一 JSON 结构

- scandeps_eval: 分析 Perl 代码片段的依赖
  参数：perl_code - Perl 代码字符串
  返回：统一 JSON 结构

- scandeps_cache: 将结果缓存到文件
  参数：module_file - Perl 模块文件路径, cache_file - 缓存文件路径
  返回：统一 JSON 结构
"""

import subprocess
import json
from typing import List
from mcp.server.fastmcp import FastMCP, Context

mcp = FastMCP("perl-Module-ScanDeps MCP Server")

@mcp.tool()
def scandeps_bundle(module_file: str) -> dict:
    """
    使用 -B 选项生成捆绑包格式的依赖分析
    
    参数:
        module_file: Perl 模块文件路径
        
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["scandeps.pl", "-B", module_file]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def scandeps_verbose(module_file: str) -> dict:
    """
    使用 -V 选项生成详细格式的依赖分析
    
    参数:
        module_file: Perl 模块文件路径
        
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["scandeps.pl", "-V", module_file]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def scandeps_trace(module_file: str) -> dict:
    """
    使用 -T 选项生成跟踪格式的依赖分析
    
    参数:
        module_file: Perl 模块文件路径
        
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["scandeps.pl", "-T", module_file]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def scandeps_executable(module_file: str) -> dict:
    """
    使用 -x 选项分析可执行文件的依赖
    
    参数:
        module_file: Perl 模块文件路径
        
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["scandeps.pl", "-x", module_file]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def scandeps_xargs(module_file: str, xargs: str) -> dict:
    """
    使用 --xargs 选项传递额外参数给模块
    
    参数:
        module_file: Perl 模块文件路径
        xargs: 额外参数字符串
        
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["scandeps.pl", "--xargs", xargs, module_file]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def scandeps_check(module_file: str) -> dict:
    """
    使用 -c 选项检查模块语法
    
    参数:
        module_file: Perl 模块文件路径
        
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["scandeps.pl", "-c", module_file]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def scandeps_recursive(module_file: str) -> dict:
    """
    使用 -R 选项递归分析依赖
    
    参数:
        module_file: Perl 模块文件路径
        
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["scandeps.pl", "-R", module_file]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def scandeps_eval(perl_code: str) -> dict:
    """
    使用 -e 选项分析 Perl 代码片段的依赖
    
    参数:
        perl_code: Perl 代码字符串
        
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["scandeps.pl", "-e", perl_code]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def scandeps_cache(module_file: str, cache_file: str) -> dict:
    """
    使用 -C 选项将结果缓存到文件
    
    参数:
        module_file: Perl 模块文件路径
        cache_file: 缓存文件路径
        
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["scandeps.pl", "-C", cache_file, module_file]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

if __name__ == "__main__":
    mcp.run()